package com.jinyang.login

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.jinyang.baserouter.PATH_FRAGMENT_DIALOG_TEST
import com.jinyang.baserouter.log
import com.jinyang.login.databinding.FragmentDialogTestBinding

/**
 * @Author:         LiuJinYang
 * @CreateDate:     2021/12/9
 * @Description:
 */
@Route(path = PATH_FRAGMENT_DIALOG_TEST)
class TestDialogFragment : AppCompatDialogFragment() {
    private var cancelCallback: (() -> Unit)? = null
    private var confirmCallback: (() -> Unit)? = null
    private lateinit var recordsViewModel: TestDialogFragmentViewModel
    private var _binding: FragmentDialogTestBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onAttach(context: Context) {
        super.onAttach(context)
        log("TestDialogFragment.onAttach")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        log("TestDialogFragment.onCreate")
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        log("TestDialogFragment.onCreateDialog")
        return super.onCreateDialog(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        log("TestDialogFragment.onCreateView")
        initWindow()
        recordsViewModel =
            ViewModelProvider(this).get(TestDialogFragmentViewModel::class.java)

        _binding = FragmentDialogTestBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.tvContent
        recordsViewModel.text.observe(viewLifecycleOwner, {
            textView.text = it
        })

        binding.tvClose.setOnClickListener {
            //具有Fragment的一切特性 可以获取依赖的activity对象
            Toast.makeText(activity, "点击了取消", Toast.LENGTH_SHORT).show()
            cancelCallback?.invoke()
            dismiss()
        }

        binding.tvConfirm.setOnClickListener {
            Toast.makeText(activity, "点击了确定", Toast.LENGTH_SHORT).show()
            confirmCallback?.invoke()
            dismiss()
        }
        return root
    }

    /**
     * 设置Dialog弹窗表现
     */
    private fun initWindow() {
        //初始化window相关表现
        val window = dialog?.window
        //设备背景为透明（默认白色）
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        //设置window宽高(单位px)
        window?.attributes?.width = 700
        //window?.attributes?.height = 350
        //设置window位置
        window?.attributes?.gravity = Gravity.CENTER//居中
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        log("TestDialogFragment.onViewCreated")
    }

    fun setCancelCallback(cancelCallback: () -> Unit) {
        this.cancelCallback = cancelCallback
    }

    fun setConfirmCallback(confirmCallback: () -> Unit) {
        this.confirmCallback = confirmCallback
    }


    override fun onStart() {
        super.onStart()
        log("TestDialogFragment.onStart")
    }


    override fun onResume() {
        super.onResume()
        log("TestDialogFragment.onResume")
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        log("TestDialogFragment.onDismiss")
    }

    override fun onPause() {
        super.onPause()
        log("TestDialogFragment.onPause")
    }

    override fun onStop() {
        super.onStop()
        log("TestDialogFragment.onStop")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        log("TestDialogFragment.onDestroyView")
        _binding = null
    }

    override fun onDestroy() {
        super.onDestroy()
        log("TestDialogFragment.onDestroy")
    }

    override fun onDetach() {
        super.onDetach()
        log("TestDialogFragment.onDetach")
    }

    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
        log("TestDialogFragment.onCancel")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        log("TestDialogFragment.onConfigurationChanged")
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        log("TestDialogFragment.onHiddenChanged")
    }

    override fun onLowMemory() {
        super.onLowMemory()
        log("TestDialogFragment.onLowMemory")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        log("TestDialogFragment.onSaveInstanceState")
    }


}